package com.lee.controller;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Created by dgyl1 on 2017/3/11.
 */
@Controller
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public class HelloController {

    private static Logger logger = LoggerFactory.getLogger(HelloController.class);

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String printHello(HttpServletRequest request, ModelMap model) {
        model.addAttribute("msg", "Spring MVC Hello World");
        model.addAttribute("name", "lee");
        model.addAttribute("path", request.getRequestURI());

        logger.info("info Spring MVC Hel");
        logger.debug("debug Spring MV");
        logger.warn("warn Spring MVC");

        return "hello";
    }

    //the parameter was converted in initBinder
    @RequestMapping(value = "/date")
    public String date(Date date) {
        System.out.println(date);
        return "redirect:hello";
    }

    //At the time of initialization,convert the type "String" to type "date"
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @RequestMapping("/getPerson")
    public void getPerson(String name, PrintWriter pw) {
        pw.write("hello," + name);
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(HttpServletRequest req) throws Exception {
        MultipartHttpServletRequest mreq = (MultipartHttpServletRequest) req;
        MultipartFile file = mreq.getFile("file");
        String fileName = file.getOriginalFilename();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        FileOutputStream fos = new FileOutputStream(req.getSession().getServletContext().getRealPath("/") +
                "upload/" + sdf.format(new Date()) + fileName.substring(fileName.lastIndexOf('.')));
        fos.write(file.getBytes());
        fos.flush();
        fos.close();

        return "hello";
    }

    @RequestMapping(value = "/param")
    public String testRequestParam(@RequestParam(value = "id") Integer id,
                                   @RequestParam(value = "name") String name, ModelMap model) {
        System.out.println(id + " " + name);
        model.addAttribute("msg", "Spring MVC Hello World");
        model.addAttribute("name", name + "_" + id);
        if (-1 == id)
            throw new RuntimeException("id == -1");

        return "/hello";
    }

}
